package com.aco.fatureapp.printer.libs;

import java.io.IOException;
import java.io.OutputStream;

public class PrinterUtils {
	private OutputStream mOutStream;

	public PrinterUtils(OutputStream mOutStream) {
		super();
		this.mOutStream = mOutStream;
	}

	public boolean reset() throws IOException {
		byte[] bLen = new byte[1];
		byte[] data = MakeComm(0, 0, 0, bLen);
		BTO_Write(data, bLen[0]);
		return true;
	}

	public boolean feedLines(int iNum) throws IOException {

		byte[] bLen = new byte[1];
		byte[] data = MakeComm(1, iNum, 0, bLen);
		BTO_Write(data, bLen[0]);
		return true;
	}

	public boolean sendString(String str) throws IOException {
		byte[] msg = str.getBytes("gb2312");

		mOutStream.write(msg);

		return true;
	}

	public boolean sendBuffer(byte[] data) throws IOException {

		mOutStream.write(data);

		return true;
	}

	public boolean setPrintPosition(int iLeftMargin, int iWidth)
			throws IOException {
		byte[] bLen1 = new byte[1];

		byte[] data1 = MakeComm(2, iLeftMargin, 0, bLen1);

		BTO_Write(data1, bLen1[0]);

		byte[] bLen2 = new byte[1];
		byte[] data2 = MakeComm(3, iWidth, 0, bLen2);
		BTO_Write(data2, bLen2[0]);
		return true;
	}

	public boolean oppositeColor(boolean bOpposite) throws IOException {

		byte[] bLen = new byte[1];
		int iVal = 0;
		if (bOpposite) {
			iVal = 1;
		}
		byte[] data = MakeComm(4, iVal, 0, bLen);
		BTO_Write(data, bLen[0]);
		return true;
	}

	public boolean setLineSpace(int iLines) throws IOException {
		byte[] bLen = new byte[1];
		byte[] data = MakeComm(5, iLines, 0, bLen);
		BTO_Write(data, bLen[0]);
		return true;
	}

	public boolean alignType(int alignType) throws IOException {

		byte[] bLen = new byte[1];
		byte[] data = MakeComm(6, alignType, 0, bLen);
		BTO_Write(data, bLen[0]);
		return true;
	}

	public boolean formatString(boolean width, boolean height, boolean bold,
			boolean underline, boolean minifont) throws IOException {
		if ((!width) && (!height) && (!bold) && (!underline) && (!minifont)) {
			return true;
		}
		byte[] bLen = new byte[1];
		int datacom = MakeFormat(width, height, bold, underline, minifont);
		byte[] data = MakeComm(13, datacom, 0, bLen);
		BTO_Write(data, bLen[0]);
		return true;
	}

	public boolean duplePrint(boolean bdup) throws IOException {
		byte[] bLen = new byte[1];
		int iVal = 0;
		if (bdup) {
			iVal = 1;
		}
		byte[] data = MakeComm(7, iVal, 0, bLen);
		BTO_Write(data, bLen[0]);
		return true;
	}

	public boolean printBarcode(int iType, int iWidth, int iHeight, int hri,
			String strData) throws IOException {

		if ((iWidth <= 0) || (iWidth > 4)) {
			iWidth = 2;
		}
		byte[] bLen1 = new byte[1];
		byte[] data1 = MakeComm(10, iWidth, 0, bLen1);
		BTO_Write(data1, bLen1[0]);

		if (iHeight <= 0) {
			iHeight = 36;
		}
		byte[] bLen2 = new byte[1];
		byte[] data2 = MakeComm(11, iHeight, 0, bLen2);
		BTO_Write(data2, bLen2[0]);

		if ((hri < 0) || (hri > 2)) {
			hri = 2;
		}
		byte[] bLen3 = new byte[1];
		byte[] data3 = MakeComm(12, hri, 0, bLen3);
		BTO_Write(data3, bLen3[0]);

		byte[] bLen4 = new byte[1];
		byte[] data4 = MakeComm(8, iType, strData.length(), bLen4);
		BTO_Write(data4, bLen4[0]);

		byte[] bLen5 = strData.getBytes();
		BTO_Write(bLen5, bLen5.length);

		return true;
	}

	public boolean printAndLineFeed() throws IOException {
		byte[] data = new byte[1];
		data[0] = 0x0A;

		BTO_Write(data, 1);
		return true;
	}

	public boolean goBlackMark() throws IOException {
		byte[] data = new byte[2];
		data[0] = 0x1D;
		data[1] = 0x0C;

		BTO_Write(data, 2);
		return true;
	}

	public boolean setPositionAbsolute(Integer pos) throws IOException {

		Integer valor = pos * 8;

		byte[] data = new byte[4];
		data[0] = 0x1B;
		data[1] = '$';
		data[2] = valor.byteValue();
		data[3] = 0;

		BTO_Write(data, 4);
		return true;
	}

	public boolean rotate80Graus() throws IOException {
		// 3.2.7 - ESC V n

		byte[] data = new byte[3];
		data[0] = 0x1B;
		data[1] = 'V';
		data[2] = 1;

		BTO_Write(data, 3);
		return true;
	}

	public boolean sendDensity(int i) throws IOException {

		byte[] data = new byte[3];
		data[0] = 0x1D;
		data[1] = 0x45;
		data[2] = (byte) i;

		BTO_Write(data, 3);
		return true;
	}

	/*
	 * public static final int BTP_reset = 0; public static final int BTP_feed =
	 * 1; public static final int BTP_left = 2; public static final int
	 * BTP_width = 3; public static final int BTP_opp = 4; public static final
	 * int BTP_linesp = 5; public static final int BTP_align = 6; public static
	 * final int BTP_dup = 7; public static final int BTP_barcode = 8; public
	 * static final int BTP_status = 9; public static final int BTP_barWidth =
	 * 10; public static final int BTP_barHeight = 11; public static final int
	 * BTP_barHRI = 12; public static final int BTP_format = 13;
	 */

	public static int MakeFormat(boolean width, boolean height, boolean bold,
			boolean underline, boolean minifont) {
		byte[] com = new byte[1];
		com[0] = 0;
		if (width) {
			int tmp17_16 = 0;
			byte[] tmp17_14 = com;
			tmp17_14[tmp17_16] = ((byte) (tmp17_14[tmp17_16] | 0x20));
		}
		if (height) {
			int tmp31_30 = 0;
			byte[] tmp31_28 = com;
			tmp31_28[tmp31_30] = ((byte) (tmp31_28[tmp31_30] | 0x10));
		}
		if (bold) {
			int tmp45_44 = 0;
			byte[] tmp45_42 = com;
			tmp45_42[tmp45_44] = ((byte) (tmp45_42[tmp45_44] | 0x8));
		}
		if (underline) {
			int tmp60_59 = 0;
			byte[] tmp60_57 = com;
			tmp60_57[tmp60_59] = ((byte) (tmp60_57[tmp60_59] | 0x80));
		}
		if (minifont) {
			int tmp76_75 = 0;
			byte[] tmp76_73 = com;
			tmp76_73[tmp76_75] = ((byte) (tmp76_73[tmp76_75] | 0x1));
		}
		return com[0];
	}

	public static byte[] MakeComm(int iType, int para1, int para2,
			byte[] iLength) {
		byte[] mBuff = new byte[20];
		int mLen = 0;

		switch (iType) {
		case 0:
			mBuff[0] = 27;
			mBuff[1] = 64;
			mLen = 2;
			break;
		case 1:
			mBuff[0] = 27;
			mBuff[1] = 74;
			mBuff[2] = ((byte) para1);
			mLen = 3;
			break;
		case 2:
			mBuff[0] = 29;
			mBuff[1] = 76;
			mBuff[2] = ((byte) (para1 / 256));
			mBuff[3] = ((byte) (para1 % 256));
			mLen = 4;
			break;
		case 3:
			mBuff[0] = 29;
			mBuff[1] = 87;
			mBuff[2] = ((byte) (para1 / 256));
			mBuff[3] = ((byte) (para1 % 256));
			mLen = 4;
			break;
		case 4:
			mBuff[0] = 29;
			mBuff[1] = 66;
			mBuff[2] = ((byte) para1);
			mLen = 3;
			break;
		case 5:
			if (1 == para2) {
				mBuff[0] = 27;
				mBuff[1] = 50;
				mLen = 2;
			}
			else {
				mBuff[0] = 27;
				mBuff[1] = 51;
				mBuff[2] = ((byte) para1);
				mLen = 3;
			}
			break;
		case 6:
			mBuff[0] = 27;
			mBuff[1] = 97;
			mBuff[2] = ((byte) para1);
			mLen = 3;
			break;
		case 7:
			mBuff[0] = 27;
			mBuff[1] = 71;
			mBuff[2] = ((byte) para1);
			mLen = 3;
			break;
		case 8:
			mBuff[0] = 29;
			mBuff[1] = 107;
			mBuff[2] = ((byte) para1);
			mBuff[3] = ((byte) para2);
			mLen = 4;
			break;
		case 10:
			mBuff[0] = 29;
			mBuff[1] = 119;
			mBuff[2] = ((byte) para1);
			mLen = 3;
			break;
		case 11:
			mBuff[0] = 29;
			mBuff[1] = 104;
			mBuff[2] = ((byte) para1);
			mLen = 3;
			break;
		case 12:
			mBuff[0] = 29;
			mBuff[1] = 72;
			mBuff[2] = ((byte) para1);
			mLen = 3;
			break;
		case 9:
			mBuff[0] = 27;
			mBuff[1] = 118;
			mLen = 2;
			break;
		case 13:
			mBuff[0] = 27;
			mBuff[1] = 33;
			mBuff[2] = ((byte) para1);
			mLen = 3;
			break;
		}

		iLength[0] = ((byte) mLen);
		return mBuff;
	}

	public boolean BTO_Write(byte[] buffer, int count) throws IOException {
		mOutStream.write(buffer, 0, count);
		return true;
	}

}